<?php
class control_user extends core_action{
	
	public $params;
	public $srvUser;
	
	function __construct(){
		$this->params['user'] = 'tabOn';
		$this->srvUser = new core_service_user(Qutil::deId($_COOKIE['uid']));
	}
	
	function index(){
		$this->islogin();
		
		$this->params['title'] = '个人中心';
		$this->render('default/user_index.php', $this->params);
	}
	
	function edit(){
		$this->islogin();
		if($_POST){
			$post = Qutil::filter($_POST);
			unset($post['email']);
			if($this->srvUser->edit($post) === false){
				$this->showMsg('编辑出错', 2);
			}
			$this->redirict(Qtpl::createUrl('user', 'edit'));
		}
		$this->params['info'] = $this->srvUser->get();
		$this->params['title'] = '编辑我的资料';
		$this->render('default/user_edit.php', $this->params);
	}
	
	function bugs(){
		$this->islogin();
		$model = new core_model_bug();
		$tn = $model->tableName();
		$catetn = core_lib_constant::TABLE_PRE."cate";
		$page = intval($_REQUEST['p']) ? intval($_REQUEST['p']) : 1;
		
		$condi = array('user_id'=>$this->srvUser->getId());
		$model->setPage($page);
		$model->setLimit(core_lib_constant::PAGE_NUM);
		$model->setCount(TRUE);
		$rs = $model->select($condi,"","","order by {$tn}.bid desc",array($catetn=>"{$tn}.cate_id={$catetn}.cid"));
		$this->params['bug'] = $rs->items;
		$this->params['pageSize'] = $rs->pageSize;
		$this->params['page'] = $page;
		$this->params['title'] = '我提交的漏洞';
		$this->render('default/user_bugs.php', $this->params);
	}
	
	function gift(){
		$this->islogin();
		$model = new core_model_order();
		$page = intval($_REQUEST['p']) ? intval($_REQUEST['p']) : 1;
		$model->setPage($page);
		$model->setLimit(core_lib_constant::PAGE_NUM);
		$model->setCount(TRUE);
		$condi = array('user_id'=>$this->srvUser->getId());
		$rs = $model->select($condi,"","","order by gid desc");
		$this->params['gift'] = $rs->items;
		$this->params['pageSize'] = $rs->pageSize;
		$this->params['page'] = $page;
		$this->params['title'] = '已兑换礼物';
		$this->render('default/user_gift.php', $this->params);
	}
	
	function notice(){
		$this->islogin();
		
		$this->params['title'] = '通知';
		$this->render('default/user_notice.php', $this->params);
	}

	function changepwd(){
		$this->islogin();
		
		$this->params['title'] = '修改密码';
		$this->render('default/user_changepwd.php', $this->params);
	}
	function register(){
		if($this->srvUser->isLogin()) $this->redirict(Qtpl::createUrl('user', 'index'));
		if($_POST){
			if($this->formToken(2) === false){
				$this->showMsg('参数错误', 2, Qtpl::createUrl('user', 'register'));
			}
			$username = Qutil::filter($_POST['username']);
			$pwd = Qutil::filter($_POST['pwd']);
			$spwd = Qutil::filter($_POST['spwd']);
			$email = Qutil::filter($_POST['email']);
			if(Qutil::isUsername($username) === false){
				$this->showMsg('用户名格式错误', 2);
			}
			if(Qutil::validEmail($email) === false){
				$this->showMsg('邮箱格式错误', 2);
			}
			if($spwd != $pwd){
				$this->showMsg('两次密码不一致', 2);
			}
			if(empty($username) or empty($pwd)){
				$this->showMsg('请输入帐号和密码', 2);
			}
			if($this->srvUser->register($username, $pwd, $email) === false){
				$this->showMsg('注册失败:'.$this->srvUser->getError(), 2);
			}
			$this->showMsg('恭喜您注册成功',1 ,Qtpl::createUrl('user', 'index'));
		}
		$this->params['token'] = $this->formToken();
		$this->params['title'] = '用户注册';
		$this->render('default/user_register.php', $this->params);
	}
	
	function login(){
		if($this->srvUser->isLogin()) $this->redirict(Qtpl::createUrl('user', 'index'));
		if($_POST){
			if($this->formToken(2) === false){
				$this->showMsg('参数错误', 2, Qtpl::createUrl('user', 'login'));
			}
			$username = Qutil::filter($_POST['username']);
			$pwd = Qutil::filter($_POST['pwd']);
			if(empty($username) or empty($pwd)){
				$this->showMsg('请输入帐号和密码', 2);
			}
			if($this->srvUser->login($username, $pwd) === false){
				$this->showMsg('用户名或者密码错误', 2);
			}
			$this->redirict(Qtpl::createUrl('user', 'index'));
		}
		$this->params['token'] = $this->formToken();
		$this->params['title'] = '用户登录';
		$this->render('default/user_login.php', $this->params);
	}
	
	function logout(){
		$this->srvUser->logout();
		$this->redirict(Qtpl::createUrl('index', 'index'));
	}
	
	protected function islogin(){
		if($this->srvUser->isLogin() === false){
			$this->redirict(Qtpl::createUrl('user', 'login'));
		}
	}
}